package com.wjh.anyview.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wjh.anyview.dao.*;
import com.wjh.anyview.entity.bo.courseServiceBo.InstitutionGradeBo;
import com.wjh.anyview.entity.dto.CommonResult;
import com.wjh.anyview.entity.po.*;
import com.wjh.anyview.entity.vo.courseControllerVo.CourseInfoVo;
import com.wjh.anyview.entity.vo.managementControllerVo.ClazzVo;
import com.wjh.anyview.service.CourseService;
import com.wjh.anyview.service.constant.StringConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author wjh
 * @date 2021/10/9 21:05
 * @Package com.wjh.anyview.service.impl
 */
@Service
@Slf4j
public class CourseServiceImpl implements CourseService {
    @Resource
    private CourseMapper courseMapper;

    @Resource
    private UserMapper userMapper;

    @Resource
    private InstitutionMapper institutionMapper;

    @Resource
    private ClazzCourseMapper clazzCourseMapper;

    @Resource
    private ClazzMapper clazzMapper;

    @Resource
    private ExamCourseMapper examCourseMapper;

    @Override
    @Transactional
    public CommonResult<?> saveCourse(Course course) {
        //查重
        QueryWrapper<Course> wrapper = new QueryWrapper<>();
        wrapper.eq("course_name", course.getCourseName());
        wrapper.eq("grade", course.getGrade());
        wrapper.eq("institution_id", course.getInstitutionId());
        //重复添加，无效
        if (courseMapper.selectOne(wrapper) != null) {
            return CommonResult.operateFailWithMessage("已存在该课程，不能重复添加！");
        }

        //添加成功
        if (courseMapper.insert(course) > 0) {
            return CommonResult.operateSuccess();
        }
        return CommonResult.operateFailWithMessage(StringConstant.SYSTEM_BUSY);
    }

    @Override
    @Transactional
    public CommonResult<?> updateCourseById(Course course) {
        //课程id或教师id不合法
        if (courseMapper.selectById(course.getId()) == null || userMapper.selectById(course.getTeacherId()) == null || institutionMapper.selectById(course.getInstitutionId()) == null) {
            log.warn("数据不合法，课程id：" + course.getInstitutionId() + "，教师id：" + course.getTeacherId() + "，隶属机构：" + course.getInstitutionId());
            return CommonResult.operateFailWithMessage(StringConstant.DATA_ILLEGAL);
        }
        //更新课程
        if (courseMapper.updateById(course) > 0) {
            return CommonResult.operateSuccess();
        }
        return CommonResult.operateFailWithMessage(StringConstant.SYSTEM_BUSY);
    }

    @Override
    @Transactional
    public CommonResult<?> deleteCourseById(Long courseId) {
        //数据不合法
        if (courseMapper.selectById(courseId) == null) {
            log.info("课程id不合法：" + courseId);
            return CommonResult.operateFailWithMessage(StringConstant.DATA_ILLEGAL);
        }
        //删除课程
        courseMapper.deleteById(courseId);
        QueryWrapper<ClazzCourse> clazzCourseQueryWrapper = new QueryWrapper<>();
        clazzCourseQueryWrapper.eq("course_id", courseId);
        //删除 课程-班级 的关系
        clazzCourseMapper.delete(clazzCourseQueryWrapper);

        QueryWrapper<ExamCourse> examCourseQueryWrapper = new QueryWrapper<>();
        examCourseQueryWrapper.eq("course_id", courseId);
        //删除这门课对应的考试
        examCourseMapper.delete(examCourseQueryWrapper);

        return CommonResult.operateSuccess();
    }

    @Override
    public CommonResult<List<CourseInfoVo>> listCourseByInstitutionGrade(InstitutionGradeBo institutionGradeBo) {
        QueryWrapper<Course> wrapper = new QueryWrapper<>();
        wrapper.eq("institution_id", institutionGradeBo.getInstitutionId());
        wrapper.eq("grade", institutionGradeBo.getGrade());
        List<Course> courseList = courseMapper.selectList(wrapper);

        Institution institution = institutionMapper.selectById(institutionGradeBo.getInstitutionId());
        String institutionName = institution.getSchool() + " " + institution.getCollege();

        List<CourseInfoVo> courseInfoVoList = new ArrayList<>();
        courseList.forEach(course -> {
            CourseInfoVo courseInfoVo = new CourseInfoVo();
            //返回courseId
            courseInfoVo.setCourseId(course.getId());
            //返回courseName
            courseInfoVo.setCourseName(course.getCourseName());
            //返回grade
            courseInfoVo.setGrade(course.getGrade());

            User user = userMapper.selectById(course.getTeacherId());
            //返回teacherId
            courseInfoVo.setTeacherId(course.getTeacherId());
            //返回teacherName
            courseInfoVo.setTeacherName(user.getName());
            //返回InstitutionName
            courseInfoVo.setInstitutionName(institutionName);
            //查询拥有该课程的班级列表
            List<ClazzVo> clazzVoList = new ArrayList<>();
            QueryWrapper<ClazzCourse> clazzCourseQueryWrapper = new QueryWrapper<>();
            clazzCourseQueryWrapper.eq("course_id", course.getId());
            //拥有该课程的 课程-班级 关系
            StringBuilder clazzNameList = new StringBuilder();
            List<ClazzCourse> clazzCourseList = clazzCourseMapper.selectList(clazzCourseQueryWrapper);
            clazzCourseList.forEach(clazzCourse -> {
                Clazz clazz = clazzMapper.selectById(clazzCourse.getClazzId());
                ClazzVo clazzVo = new ClazzVo();
                clazzVo.setClazzId(clazz.getId());
                clazzVo.setClazzName(clazz.getClazzName());
                clazzNameList.append(clazz.getClazzName()).append(" ");
                clazzVoList.add(clazzVo);
            });
            //返回拥有该课程的班级列表
            courseInfoVo.setClazzNameList(clazzNameList.toString());
            courseInfoVo.setClazzVoList(clazzVoList);
            courseInfoVoList.add(courseInfoVo);
        });
        return CommonResult.operateSuccess(courseInfoVoList);
    }

}
